package com.dlb.andorid.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlb.andorid.App;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.bean.UserInfo;
import com.dlb.andorid.presenter.LoginPresenter;
import com.dlb.andorid.wxapi.WXEntryActivity;
import com.kwan.xframe.BaseApplication;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;
import com.kwan.xframe.util.SPUtil;
import com.tencent.mm.opensdk.modelbase.BaseResp;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import static com.dlb.andorid.presenter.LoginPresenter.CODE_TYPE_LOGIN;
import static com.dlb.andorid.presenter.LoginPresenter.LOGIN_TYPE_PWD;
import static com.dlb.andorid.presenter.LoginPresenter.LOGIN_TYPE_WX;

public class LoginActivity extends BaseCommonActivity implements XContract.ILoginView {

	LoginPresenter mPresenter;

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new LoginPresenter(this);
		return mPresenter;
	}


	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_login;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		return null;
	}

	@Override
	public String getPageName() {
		return null;
	}


	private TextView tv_login_by_phone, tv_login_by_account, tv_labe_phone, tv_labe_code;
	//private Group group_phone, group_code;
	private ImageView iv_labe_phone, iv_login_wx;
	private EditText et_phone, et_code;
	private TextView tv_get_code;
	private View tv_forget, tv_reg, tv_quick_login, tv_login, iv_pwd_visible;

	@Override
	protected void initViews() {
		super.initViews();

		iv_login_wx = findViewById(R.id.iv_login_wx);

		tv_get_code = findViewById(R.id.tv_get_code);
		et_code = findViewById(R.id.et_code);

		iv_pwd_visible = findViewById(R.id.iv_pwd_visible);

		tv_login_by_phone = findViewById(R.id.tv_login_by_phone);
		tv_login_by_account = findViewById(R.id.tv_login_by_account);
		tv_quick_login = findViewById(R.id.tv_quick_login);
		tv_forget = findViewById(R.id.tv_forget);
		tv_reg = findViewById(R.id.tv_reg);

		tv_login = findViewById(R.id.tv_login);

		et_phone = findViewById(R.id.et_phone);
		tv_labe_phone = findViewById(R.id.tv_labe_phone);
		tv_labe_code = findViewById(R.id.tv_labe_code);


		iv_labe_phone = findViewById(R.id.iv_labe_phone);

		tv_login_by_phone.setOnClickListener(this);
		tv_login_by_account.setOnClickListener(this);
		tv_forget.setOnClickListener(this);
		tv_reg.setOnClickListener(this);
		tv_quick_login.setOnClickListener(this);

		tv_get_code.setOnClickListener(this);
		tv_login.setOnClickListener(this);

		iv_login_wx.setOnClickListener(this);

		onClick(tv_login_by_account);
	}


	void setGroupPhone(int v) {
		tv_get_code.setVisibility(v);
		tv_quick_login.setVisibility(v);
	}

	void setGroupCode(int v) {
		iv_pwd_visible.setVisibility(v);
		tv_login.setVisibility(v);
		tv_reg.setVisibility(v);
		tv_forget.setVisibility(v);
	}

	@Override
	protected void beForeSetContentView() {
		super.beForeSetContentView();
		registPayResultBroadcast();
	}

	boolean isAutoLogin;

	@Override
	protected void initData() {

//		Calendar calendar = Calendar.getInstance();
//		calendar.clear();
//		calendar.set(2020, 0, 1);
//		long millis = calendar.getTimeInMillis();
//		Log.e("kwan", "final time:" + millis);
//		Log.e("kwan", "current time:" + System.currentTimeMillis());
//		if (millis < System.currentTimeMillis()) {
//			finish();
//		}


		Log.e("kwan", "SPUtil.getUserAccount():" + SPUtil.getUserAccount());
		Log.e("kwan", "SPUtil.getUserPwd():" + SPUtil.getUserPwd());

		switch (SPUtil.getLoginType()){

			case LOGIN_TYPE_PWD:

				if (SPUtil.getUserAccount() != null && !SPUtil.getUserAccount().isEmpty()) {
					if (SPUtil.getUserPwd() != null && !SPUtil.getUserPwd().isEmpty()) {
						showProgress("");
						isAutoLogin = true;
						mPresenter.loginByPwd(SPUtil.getUserAccount(), SPUtil.getUserPwd());
					}
				}

				break;

			case LOGIN_TYPE_WX:
				showProgress("");
				isAutoLogin = true;
				mPresenter.loginByWx(SPUtil.getWXOpinID());
				break;

		}



	}

	@Override
	public void onClick(View v) {
		super.onClick(v);

		if (v == tv_login_by_phone) {

			tv_login_by_phone.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
			tv_login_by_account.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));//加粗
			iv_labe_phone.setImageResource(R.mipmap.ic_login_phone);
			tv_labe_phone.setText("手机号");
			tv_labe_code.setText("验证码");
			et_code.setText("");
			et_code.setHint("请输入验证码");
			et_code.setInputType(InputType.TYPE_CLASS_TEXT);
			setGroupPhone(View.VISIBLE);
			setGroupCode(View.INVISIBLE);

		} else if (v == tv_login_by_account) {

			tv_login_by_account.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
			tv_login_by_phone.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));//加粗
			iv_labe_phone.setImageResource(R.mipmap.ic_account);
			tv_labe_phone.setText("账号");
			tv_labe_code.setText("密码");
			et_code.setText("");
			et_code.setHint("密码");
			et_code.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			setGroupPhone(View.INVISIBLE);
			setGroupCode(View.VISIBLE);


		} else if (v == tv_forget) {
			Bundle bundle = new Bundle();
			bundle.putByte("mode", RegOrForgetActivity.MODE_FORGET);
			go2Activity(RegOrForgetActivity.class, bundle, false);
		} else if (v == tv_reg) {
			Bundle bundle = new Bundle();
			bundle.putByte("mode", RegOrForgetActivity.MODE_REG);
			go2Activity(RegOrForgetActivity.class, bundle, false);
		} else if (v == tv_quick_login) {

			if (checkCode() && checkPhone()) {
				showProgress("");
				mPresenter.loginByCode(mStrPhone, mStrCode);
			}

		} else if (v == tv_get_code) {
			if (checkPhone()) {
				showProgress("");
				mPresenter.getCode(mStrPhone, CODE_TYPE_LOGIN);
			}
		} else if (v == tv_login) {
			if (checkPwd() && checkPhone()) {
				showProgress("");
				mPresenter.loginByPwd(mStrPhone, mStrPwd);
			}
		} else if (v == iv_login_wx) {
			App.mWXLoginUtil.doLogin();
		}
	}

	String mStrPhone, mStrCode, mStrPwd;

	boolean checkPwd() {
		mStrPwd = et_code.getText().toString().trim();
		if (mStrPwd.isEmpty()) {
			toastMsg("请填密码");
			return false;
		} else if (mStrPwd.length() < 6) {
			toastMsg("请填6位数密码");
			return false;
		}

		return true;
	}

	boolean checkCode() {
		mStrCode = et_code.getText().toString().trim();
		if (mStrCode.isEmpty()) {
			toastMsg("请填验证码号");
			return false;
		}
		return true;
	}

	boolean checkPhone() {

		mStrPhone = et_phone.getText().toString().trim();
		if (mStrPhone.isEmpty()) {
			toastMsg("请填写手机号");
			return false;
		}
		return true;
	}

	@Override
	public void getCodeSuccess(String msg) {
		dismissProgress();
		toastMsg(msg);
	}

	@Override
	public void onLoginSuccess(UserInfo data) {
		dismissProgress();
		toastMsg("登录成功");
		App.mUserInfo = data;

		SPUtil.setLoginType(LoginPresenter.LOGIN_TYPE);

		if (!isAutoLogin) {
			SPUtil.setUserAccount(mStrPhone);
			SPUtil.setUserPwd(mStrPwd);
			SPUtil.setWXOpinID(App.mOpenid);
		}


		Bundle bundle = new Bundle();
		go2Activity(MainActivity.class, bundle, true);
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		unRegistPayResultBroadcast();
	}

	LocalBroadcastManager mBroadcastManager;

	private void registPayResultBroadcast() {
		mBroadcastManager = LocalBroadcastManager.getInstance(BaseApplication.getInstance().getApplicationContext());
		IntentFilter filter = new IntentFilter(WXEntryActivity.WECHAT_LOGIN_RESULT_ACTION);
		mBroadcastManager.registerReceiver(mReceiver, filter);
	}

	private void unRegistPayResultBroadcast() {
		if (mBroadcastManager != null && mReceiver != null) {
			mBroadcastManager.unregisterReceiver(mReceiver);
			mBroadcastManager = null;
			mReceiver = null;
		}
	}

	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			int result = intent.getIntExtra(WXEntryActivity.WECHAT_LOGIN_RESULT_EXTRA, -100);
			if (result == BaseResp.ErrCode.ERR_OK) {
				Log.e("kwan", "接收到回调...");
				showProgress("微信登录中..");
				mPresenter.loginByWx(App.mOpenid);
			}
		}
	};

}
